package com.gimi.cloud.jimi.client.service.order.impl;

import com.gimi.cloud.jimi.client.service.order.OrderDetailService;
import com.gimi.cloud.jimi.common.enums.CommonEnume;
import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.commonService.common.JmConfigService;
import com.gimi.cloud.jimi.commonService.common.JmUserService;
import com.gimi.cloud.jimi.dao.dao.order.*;
import com.gimi.cloud.jimi.dao.dao.product.ProductSkuDao;
import com.gimi.cloud.jimi.dto.order.ReplantDTO;
import com.gimi.cloud.jimi.entity.order.AccountEntity;
import com.gimi.cloud.jimi.entity.order.OrderCostEntity;
import com.gimi.cloud.jimi.entity.order.OrderEntity;
import com.gimi.cloud.jimi.dto.params.OrderParams;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 订单详情
 *
 * @author chendi
 * @date 2018年1月19日
 */
@Service
@Transactional
public class OrderDetailServiceImpl implements OrderDetailService {
    @Autowired
    private OrderCodeDao orderCodeDao;
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderGoodsDao orderGoodsDao;
    @Autowired
    private OrderExpressDao orderExpressDao;
    @Autowired
    private OrderOperDao orderOperDao;
    @Autowired
    private OrderCostDao orderCostDao;
    @Autowired
    private OrderInvoiceDao orderInvoiceDao;
    @Autowired
    private JmConfigService configService;
    @Autowired
    private JmUserService jmUserService;
    @Autowired
    private ProductSkuDao productSkuDao;
    @Autowired
    private AccountDao accountDao;
    @Autowired
    private ReplantDao replantDao;

    private static Logger logger = LoggerFactory.getLogger(OrderDetailServiceImpl.class);

    @Override
    public Map<String, Object> getOrderDetail(OrderParams orderParams) {
        if (!StringUtil.isBlank(orderParams.getoId())) {
            Map<String, Object> map = orderDao.getorderDetail(orderParams.getoId());
            //status状态转换
            try {
                String status = MapUtils.getString(map, "status");// L01
                map.put("status", CommonEnume.OrderStatus.valueOf(status).getValue());
            } catch (Exception e) {
                logger.info("该订单状态值异常!");
            }

            //sku属性转换
            String skuId = MapUtils.getString(map, "sku_id");// sku_id 1ef333
            List<String> skuList = productSkuDao.getModelValueBySkuId(skuId);
            StringBuffer sb = new StringBuffer();
            if (skuList != null && skuList.size() > 0) {
                for (String sku : skuList) {
                    sb.append(sku);
                    sb.append("/");
                }
                String skuString = sb.toString().substring(0, sb.toString().length() - 1);
                map.put("skuValue", skuString);
            }
            return map;
        } else {
            logger.info(String.format("===>没有用户%d的订单", orderParams.getoId()));
            return null;
        }

    }

    @Override
    public List<ReplantDTO> getReplantDetail(ReplantDTO replantDTO) {
        OrderEntity orderEntity = orderDao.selectByPrimaryKey(replantDTO.getoId());
        if (orderEntity == null) {
            throw new MsgException(ResponseCodeEnum.FAILURE, "该用户没有订单记录，请确认后重试！");
        }
        AccountEntity accountEntity = accountDao.selectByFcontractCode(orderEntity.getOrderCode());
        if (accountEntity == null) {
            throw new MsgException(ResponseCodeEnum.FAILURE, "该用户没有账户记录，请确认后重试！");
        }
        List<ReplantDTO> replantDTOList = replantDao.getReplantDetail(accountEntity.getAccountno());
        OrderCostEntity orderCostEntity = null;
        for (ReplantDTO replantDTOTemp : replantDTOList) {
            //首期=保障金+保险费+应付租金
            //其他=应付租金
            if (replantDTOTemp.getFperiod() == 1) {
                orderCostEntity = orderCostDao.selectOrderCostByOrderCode(orderEntity.getOrderCode());
                replantDTOTemp.setDeposit(orderCostEntity.getDeposit());
                replantDTOTemp.setPremium(orderCostEntity.getPremium());
                replantDTOTemp.setTotalfrecel(replantDTOTemp.getFrecel().add(orderCostEntity.getDeposit()).add(orderCostEntity.getPremium()));
            }
            //待支付金额 =实际应付-减免金额-已支付金额
            replantDTOTemp.setTobefrecl(replantDTOTemp.getTotalfrecel().subtract(replantDTOTemp.getFexempt()).subtract(replantDTOTemp.getFreal()));
        }
        return replantDTOList;
    }


}
